What are subscriptions in Knockout.js?
195
28-Jun-2024
Updated on 28-Jun-2024
Ashutosh Kumar Verma
28-Jun-2024Knockout.js Subscribe() method
In Knockout.js, a subscription refers to a mechanism that allows you to track observable changes (which allows you to track changes to properties). Subscriptions enable you to respond to changes in observables by executing custom logic each time the value of an observable changes.
Here is how subscriptions work in Knockout.js,
Creating a Subscription
You can create a subscription for an observable by calling a
subscribe
method on the observable. Thesubscribe
method takes a callback function as an argument. This callback function will be called whenever the value of the observable changesAutomatic Execution
When you use that function to create a new value of an observable (e.g.,
myObservable(newValue)
), Knockout.js automatically notifies all clients by calling their callback functionExample-
Output-
Also, Read: Explain Inline vs. External Templates in knockout.js.